package activities;

import java.util.Calendar;
import java.util.Date;

import orm.FitnessData;
import orm.FitnessDataDBHelper;

import com.example.intellifit.R;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class FitnessDataActivity extends Activity {

	private Button saveFitnessDataBtn, loadFitnessDataBtn, pickDateBtn;
	private EditText notesTxt, heightTxt, weightTxt, fitnessTxt, motivationTxt, loadId, dateText;
	private TextView personLable;
    private int mYear, mMonth, mDay;
    static final int DATE_DIALOG_ID = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fitness_data);

		init();
	}

	private void init(){

		notesTxt = (EditText) findViewById(R.id.fitnessData_notesText);
		heightTxt = (EditText) findViewById(R.id.fitnessData_heightText);
		weightTxt = (EditText) findViewById(R.id.fitnessData_weightText);
		fitnessTxt = (EditText) findViewById(R.id.fitnessData_fitnessText);
		motivationTxt = (EditText) findViewById(R.id.fitnessData_motivationText);
		loadId = (EditText) findViewById(R.id.fitnessData_personText);
		personLable = (TextView) findViewById(R.id.fitnessData_personLable);
		loadId.setVisibility(View.INVISIBLE);
		personLable.setVisibility(View.INVISIBLE);
		
		saveFitnessDataBtn = (Button) findViewById(R.id.saveFitnessDataBtn);
	
		saveFitnessDataBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d("SAVE Account: ", "Saving ..");
				save();

			}
		});
		
		loadFitnessData(); //default
		
		loadFitnessDataBtn = (Button) findViewById(R.id.loadFitnessDataBtn);

		loadFitnessDataBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d("Load Fitness Data: ", "Doading FD ..");
				loadFitnessData();

			}
		});
		
		
		/*   Date   */
        // capture our View elements
		pickDateBtn = (Button) findViewById(R.id.pickDate);

        // add a click listener to the button
		pickDateBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        // display the current date (this method is below)
        updateDisplay();
		
	}
	
	/*
	 * Loads the fitnessdata with the id of the edit field
	 */
	private void loadFitnessData(){
		// Check if it exists a user for this device
		FitnessData fd = FitnessDataDBHelper.getFitnessDataByID(this, 1);
		if (fd != null) {
			render(fd);
		}
	}
	
	/*
	 * Renders the person to the Android view
	 * notesTxt, heightTxt, weightTxt, fitnessTxt, motivationTxt, loadId;
	 * 
	 */
	private void render(FitnessData fd) {

		if (fd != null) {
			if (fd.getNotes() != null) {
				notesTxt.setText("" + fd.getNotes());
			}
				heightTxt.setText(""+fd.getHight());
				weightTxt.setText(""+fd.getWeight());
				fitnessTxt.setText(""+fd.getFitness());
				motivationTxt.setText(""+fd.getMotivation());
				loadId.setText(""+fd.getID());
		}
	}
	
	/**
	 * Save the FitnessData
	 */
	public void save() {

		Date d = new Date();

		FitnessData fd = new FitnessData(
				1, 
				1, 
				notesTxt.getText().toString(),
				d, 
				Integer.valueOf(heightTxt.getText().toString()),
				Integer.valueOf(weightTxt.getText().toString()),
				Integer.valueOf(fitnessTxt.getText().toString()),
				Integer.valueOf(motivationTxt.getText().toString()));
		
		long id = FitnessDataDBHelper.saveFitnessData(this, fd);

	}
	
	
	/* Date */
    // updates the date in the TextView
    private void updateDisplay() {
    	pickDateBtn.setText(getString(R.string.strSelectedDate,
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).append("-")
                    .append(mYear).append(" ")));
    }
    
    // the callback received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
            };
            
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);
		}
		return null;
	}
	
	/*Date*/
}
